package com.xinqi.modules.sys.sensitive.word.component;

import com.xinqi.common.base.constants.SensitiveWordConstant;
import com.xinqi.common.core.utils.SpringUtils;
import com.xinqi.common.redis.utils.Caches;
import com.xinqi.modules.sys.sensitive.word.domain.SensitiveWordEntity;
import com.xinqi.modules.sys.sensitive.word.dto.rsp.SensitiveWordResultDTO;
import com.xinqi.modules.sys.sensitive.word.service.SensitiveWordService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.redisson.api.RedissonClient;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Component
@RequiredArgsConstructor
public class SensitiveWordSubjectManager implements ApplicationRunner {
    @Lazy
    private final SensitiveWordService sensitiveWordService;

    private static final RedissonClient redissonClient = SpringUtils.getBean(RedissonClient.class);

    @PostConstruct
    public void init() {
        Caches.del(redissonClient, SensitiveWordConstant.SENSITIVE_WORD_KEY);
        Set<String> wordsSet = loadSensitiveWordsFromDatabase();
        Caches.set(wordsSet, SensitiveWordConstant.CacheDuration.DECADE, redissonClient, SensitiveWordConstant.SENSITIVE_WORD_KEY);
    }

    // 启动时加载敏感词数据到内存
    public Set<String> loadSensitiveWordsFromDatabase() {
        Set<String> wordsSet = new HashSet<>();

        List<SensitiveWordResultDTO> sensitiveList = sensitiveWordService.listEffective();
        if (CollectionUtils.isNotEmpty(sensitiveList)) {
            for (SensitiveWordResultDTO sensitiveWord : sensitiveList) {
                wordsSet.addAll(Arrays.asList(sensitiveWord.getContent().split(",")));
            }
        }

        return wordsSet;
    }

    // 敏感词创建
    public void notifyOnCreate(SensitiveWordEntity entity) {
        Set<String> wordsSet = loadSensitiveWordsFromDatabase();
        if (CollectionUtils.isNotEmpty(wordsSet)) {
            Caches.set(wordsSet, SensitiveWordConstant.CacheDuration.DECADE, redissonClient, SensitiveWordConstant.SENSITIVE_WORD_KEY);
        }
    }

    // 敏感词更新
    public void notifyOnUpdate(SensitiveWordEntity entity) {
        Caches.del(redissonClient, SensitiveWordConstant.SENSITIVE_WORD_KEY);

        Set<String> wordsSet = loadSensitiveWordsFromDatabase();
        if (CollectionUtils.isNotEmpty(wordsSet)) {
            Caches.set(wordsSet, SensitiveWordConstant.CacheDuration.DECADE, redissonClient, SensitiveWordConstant.SENSITIVE_WORD_KEY);
        }
    }

    // 敏感词删除
    public void notifyOnDelete(SensitiveWordEntity entity) {
        Caches.del(redissonClient, SensitiveWordConstant.SENSITIVE_WORD_KEY);

        Set<String> wordsSet = loadSensitiveWordsFromDatabase();
        if (CollectionUtils.isNotEmpty(wordsSet)) {
            Caches.set(wordsSet, SensitiveWordConstant.CacheDuration.DECADE, redissonClient, SensitiveWordConstant.SENSITIVE_WORD_KEY);
        }
    }

    @Override
    public void run(ApplicationArguments args) {

    }
}
